$(document).ready(function() {
	/* Global variables for math operations*/
	var first_op = -1;
	var second_op = -1;
	var op_type = 0;

	/* Bind event for number buttons */
	$("#numbers button").bind('click', function (event) {
		var current = $(this).html();
		var prev = $("#result input[name='numbers']").val();
		$("#result input[name='numbers']").val( prev + current);
	});

	/* Bind event for operation buttons */
	$("#operations button").bind('click', function (event) {
		first_op = $("#result input[name='numbers']").val(); 
		$("#result input[name='numbers']").val("");
		
		switch ( $(this).html() ) {
			case '+':
				op_type = 'add';
				break;

			case '-':
				op_type = 'sup';
				break;

			case '/':
				op_type = 'div';
				break;

			case '*':
				op_type = 'mul';
				break;
		}
	});

	/* Send our request to php script and get json data */
	function send() {
		var url_gen = 'req.php?fop=' + first_op + '&sop='
			+ second_op + '&type=' + op_type;

		$.ajax({
			dataType: 'json',
			url: url_gen,
			success: function(data) {
				$("#result input[name='numbers']").val(data.result);
			} 
		});
	}

	/* Get result */
	$("#equal button").bind('click', function() {
		second_op = $("#result input[name='numbers']").val();
		$("#result input[name='numbers']").val("Опрацювання...");
		send();
	});
});



